﻿Public MustInherit Class ItemListPage
    Inherits ItemPage

    Protected MustOverride ReadOnly Property DefaultOrder() As OrderCriterion

    Protected MustOverride ReadOnly Property DefaultFilter() As FilterCriterion

    Protected MustOverride ReadOnly Property CreatePageURL() As String

    Protected WithEvents ListGrid As ListView

    Protected WithEvents SearchType As DropDownList

    Protected WithEvents SearchBox As TextBox

    Protected WithEvents SearchButton As Button

    Protected WithEvents ResetButton As Button

    Protected WithEvents CreateButton As Button

    Protected MustOverride Function CreateDataGateway() As DataGateway

    Protected Property OrderBy() As OrderCriterion
        Get
            If ViewState("OrderBy") Is Nothing Then
                ViewState("OrderBy") = DefaultOrder
            End If
            Return ViewState("OrderBy")
        End Get
        Set(value As OrderCriterion)
            ViewState("OrderBy") = value
        End Set
    End Property

    Protected Property FilterBy() As FilterCriterion
        Get
            If ViewState("FilterBy") Is Nothing Then
                ViewState("FilterBy") = DefaultFilter
            End If
            Return ViewState("FilterBy")
        End Get
        Set(value As FilterCriterion)
            ViewState("FilterBy") = value
        End Set
    End Property

    Protected Overridable Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ResetFilter()
            UpdateDataSource()
            UpdateVisibility()
        End If
    End Sub

    Protected Sub SearchBox_TextChanged(sender As Object, e As EventArgs) Handles SearchBox.TextChanged
        UpdateFilter()
        UpdateDataSource()
    End Sub

    Protected Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        UpdateFilter()
        UpdateDataSource()
    End Sub

    Protected Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
        Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath)
    End Sub

    Protected Sub CreateButton_Click(sender As Object, e As EventArgs) Handles CreateButton.Click
        Response.Redirect(CreatePageURL)
    End Sub

    Protected Sub UpdateFilter()
        FilterBy = New FilterCriterion(SearchType.SelectedValue, SearchBox.Text)
    End Sub

    Protected Sub ResetFilter(Optional ByVal filter As FilterCriterion = Nothing)
        FilterBy = filter
        If SearchType IsNot Nothing Then
            SearchType.SelectedValue = FilterBy.Column
            SearchBox.Text = FilterBy.Value
        End If
    End Sub

    Protected Sub ListGrid_Sorting(sender As Object, e As ListViewSortEventArgs) Handles ListGrid.Sorting
        If e.SortExpression = OrderBy.Expression Then
            OrderBy.ToggleDirection()
        Else
            OrderBy = New OrderCriterion(e.SortExpression)
        End If
        UpdateDataSource()
    End Sub

    Protected Overridable Sub UpdateDataSource()
        Dim gateway As DataGateway = CreateDataGateway()
        If String.IsNullOrWhiteSpace(FilterBy.Value) Then
            ListGrid.DataSource = gateway.FindAll(OrderBy)
        Else
            ListGrid.DataSource = gateway.FindWhere(FilterBy, OrderBy)
        End If
        ListGrid.DataBind()
    End Sub

    Protected Sub UpdateVisibility()
        If CreateButton IsNot Nothing Then
            CreateButton.Visible = InsertionPermitted
        End If
    End Sub
End Class
